package my.test.websocket;

import org.java_websocket.enums.ReadyState;

import java.net.URI;

/**
 * @author yuhang
 * @desc
 * @since 2022/11/4
 */
public class ClientTest2 {

    public static void main(String[] args) {
        try {
            MyWebSocketClient myClient = new MyWebSocketClient(new URI("ws://bz.go2b2b.com/api/live/b/89999"));
            myClient.connect();
            while (!myClient.getReadyState().equals(ReadyState.OPEN)) {
                System.out.println("连接中。。。");
                Thread.sleep(1000);
                if (!myClient.isOpen()) {
                    if (myClient.getReadyState().equals(ReadyState.NOT_YET_CONNECTED)) {
                        try {
                            myClient.connect();
                        } catch (IllegalStateException e) {
                        }
                    } else if (myClient.getReadyState().equals(ReadyState.CLOSING) || myClient.getReadyState().equals(ReadyState.CLOSED)) {
                        myClient.reconnect();
                    }
                }
            }


            while (true) {
                System.out.println("进入定时器");
                if (myClient.isClosed()) {
                    System.out.println("断线重连");
                    myClient.reconnect();
                }
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
